
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Xml;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Container;
using Swaf.Security;

namespace Articles
{
	public class ArticleAssociationBizRules : Syrinx.BaseBizRules
	{
		/// <summary>
		/// 
		/// </summary>
		/// <param name="app"></param>
		/// <param name="context"></param>
		/// <param name="info">
		///  valid info fields articleId,relatedItemId,associationName
		/// </param>
		public void createArticleAssociation(IApplication app, IRuleContext context, INameAddressSupport info)
		{
			try
			{
				if (info != null)
				{
					info.put("companyId", ActiveCompanyId);
					if (Cmn.IsEmpty(info.get("associationName")))
						info.put("associationName", "eComProd");
					app.appData.put("DA.Articles.AddArticleAssociation", info);
				}
			}
			catch (Exception e)
			{
				throw new ArticlesException("error executing createArticleAssociation.", e);
			}
		}

		public IBizObj checkForAssociatedArticleCreateIfNeeded(IApplication app, IRuleContext context, string associationName, Swaf.BGuid.Guid associatedId,
			string newArticleTitle, string newArticleUniqueTitle, string parentArticleUniqueName)
		{
			context.put("companyId", ActiveCompanyId);
			context.put("associationName", associationName);
			context.put("relatedItemId", associatedId);
			context.put("details", DetailLevel.idOnly);
			IBizObj article = app.appData.get("DA.Articles.GetAssociatedArticleDetails", context) as IBizObj;
			if (article == null)
			{
				article = app.bizObjMgr.create("Article");
				article.put("id", new Swaf.BGuid.Guid());
				article.put("baseTitle", newArticleTitle);
				article.put("uniqueTitle", newArticleUniqueTitle);

				if (parentArticleUniqueName != null)
				{
					context.put("uniqueTitle", parentArticleUniqueName);
					IBizObj parentArticle = app.appData.get("DA.Articles.GetArticleByUniqueTitle", context) as IBizObj;
					article.put("parentId", parentArticle.get("id"));
				}
				IResults rc = app.ruleMgr.execute(context, "updateArticle", article);

				context.put("articleId", article.get("id"));
				app.appData.put("DA.Articles.AddArticleAssociation", context);
			}
			return article;
		}
		
		public IBizObjList getArticlesAssociatedWithId(IApplication app, IRuleContext context, string associationName, Swaf.BGuid.Guid associatedId)
		{
			context.put("relatedItemId", associatedId);
			context.put("associationName", associationName);
			IBizObjList articles = app.appData.get("DA.Articles.GetArticlesAssociateWithIdName", context) as IBizObjList;
			return articles;
		}

		public IBizObjList getAllAssociationsToArticle(IApplication app, IRuleContext context, Swaf.BGuid.Guid articleId)
		{
			IBizObjList articles = null;
			return articles;
		}

		public void addAssociationToArticle(IApplication app, IRuleContext context, string associationName, Swaf.BGuid.Guid associatedId, Swaf.BGuid.Guid articleId)
		{
			context.put("articleId", articleId);
			context.put("relatedItemId", associatedId);
			context.put("associationName", associationName);
			app.appData.put("DA.ArticlesAdmin.InsertAssociationToArticle", context);
		}

		public void removeAssociationToArticle(IApplication app, IRuleContext context, string associationName, Swaf.BGuid.Guid associatedId, Swaf.BGuid.Guid articleId)
		{
		}

		public void removeAllAssociationsToArticle(IApplication app, IRuleContext context, Swaf.BGuid.Guid articleId)
		{
		}
	}
}
